<?php
/**
 * @file name : products.php
 * @package   : thokim
 * @author    : Vo Thanh Minh
 * @email     : <thanhminhld@yahoo.com - thanhminhld@gmail.com>
 * @copyright : 2011
 *
 */
if (!defined('BASEPATH')) {
    exit('No direct script access');
}

class Products extends Base_Controller
{
    /**
     * __construct
     * @access public
     * @param
     * @return
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
      * _remap
      * @access public
      * @param
      * @return
      */
    public function _remap( $method )
    {
        switch ($method) {

            case 'products-category':
                $this->loadProductCategory();
                break;

            case 'products-detail':
                $this->loadProductDetail();
                break;

            default:
                $this->index();
        }
    }
    /**
     * @function index
     * @access     private
     * @param
     * @return
     */
    private function index()
    {
        $controlName    = $this->uri->segment(1);

        $arrWhere = array('lang_id' => $this->lang_default);
        /**
         * Load parent category list
         */
        $modulesList = $this->products_model->loadList($arrWhere, 'all', 0, 'category_id', 'ASC');
        $arrCategory = array2ParentChildStructor($modulesList, 'parent_id');
        $arrCategoryList = array();
        if (isset($arrCategory[0])) {
            foreach($arrCategory[0] as $key => $parentCate) {
                $arrChildId = array();
                if (isset($arrCategory[$parentCate->category_id])) {
                    foreach($arrCategory[$parentCate->category_id] as $childObj) {
                        array_push($arrChildId, $childObj->category_id);
                    }
                }
                if (count($arrChildId) > 0) {
                    $arrWhere = array('lang_id'      => $this->lang_default,
                                      'product_show' => 'Y');
                    $parentCate->product = $this->productspost_model->loadListWhereIn('', 'category_id', $arrChildId,
                                                                                      $arrWhere, 'product_id',
                                                                                      'DESC', 6, 0);
                } else {
                    $parentCate->product = array();
                }
            }
            $arrCategoryList = $arrCategory[0];
        }

        $data = array('urlControlName'  => $controlName . '/',
                      'arrCategoryList' => $arrCategoryList,
                      'categoryUrl'     => $controlName . '/products-category/PC-',
                      'postUrl'         => $controlName . '/products-detail/PP-',
                      'prodImgPath'     => 'application/uploads/productpost/',);
        $this->msmarty->assignArrVal($data);

        $main = $this->msmarty->fetch($controlName . '/products.tpl');
        $this->msmarty->assign('main', $main);

        $this->msmarty->view();
    }
    /**
     * @function    loadProductCategory
     *
     * @author      Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access      private
     *
     */
    private function loadProductCategory()
    {
        $controlName    = $this->uri->segment(1);
        $method      = $this->uri->segment(2);
        $segment3    = $this->uri->segment(3);

        $categoryId  = explode('-', $segment3);
        $categoryId  = $categoryId[1];

        $lastSegment = $this->uri->segment($this->uri->total_segments());

        $prodCateObj = $this->products_model->loadRecord($categoryId, $this->lang_default);

        if ( ! is_object($prodCateObj)) {
            defaultPage($controlName);
        }
        $prodCateList = array();
        $allCategory = TRUE;
        $arrWhere = array('lang_id'   => $this->lang_default,
                          'parent_id' => $prodCateObj->category_id);
        $arrChildCategory = $this->products_model->loadList($arrWhere, 'all', 0, 'category_id', 'ASC');
        if (count($arrChildCategory) > 0) {
            // Get 6 post for each child category
            foreach ($arrChildCategory as $childCate) {
                $arrWhere = array('lang_id'      => $this->lang_default,
                                  'category_id'  => $childCate->category_id,
                                  'product_show' => 'Y');
                $arrPostList = $this->productspost_model->loadList($arrWhere, 6, 0, 'product_id', 'DESC');
                if (count($arrPostList) > 0) {
                    $childCate->product = $arrPostList;
                } else {
                    $childCate->product = array();
                }
            }
            $prodCateList = $arrChildCategory;
        } else {
            // Category don't have child -> get 6 post for this category with paging
            /**
             * Config for paging
             */
            $arrWhere = array('lang_id'      => $this->lang_default,
                              'category_id'  => $categoryId,
                              'product_show' => 'Y');
            $this->load->library('pagination');
            $config['base_url'] = 'http://' . $_SERVER["HTTP_HOST"] . dirname($_SERVER["SCRIPT_NAME"])
                                . '/' . $controlName . '/products-category/' . $segment3 . '/';
            $totalRows = $this->productspost_model->getTotalRows($arrWhere);
            $config['total_rows'] = $totalRows;
            $config['per_page'] = 4;
            $config['num_links'] = 1;
            $config['page_query_string'] = FALSE;
            $config['last_segment'] = $lastSegment;

            $config['uri_segment'] = 4;
            $config['first_link'] = 'First';
            $config['last_link'] = 'Last';
            $config['next_link'] = 'Next';
            $config['prev_link'] = 'Previous';
            $config['cur_tag_open'] = '<span class="current">';
            $config['cur_tag_close'] = '</span>';

            $this->pagination->initialize($config);
            $str_paging = $this->pagination->create_links();
            $this->msmarty->assign('paging',$str_paging);

            $offset = $this->uri->segment(4);
            if ( ! is_numeric($offset)) {
                $offset = 0;
            }
            /**
             * End paging
             */
            $prodCateObj->product = $this->productspost_model->loadList($arrWhere, $config['per_page'], $offset,
                                                                        'product_id', 'DESC');
            $prodCateList = $prodCateObj;
            $allCategory = FALSE;
        }
        $data = array('prodCateList'    => $prodCateList,
                      'categoryUrl'     => $controlName . '/products-category/PC-',
                      'postUrl'         => $controlName . '/products-detail/PP-',
                      'prodImgPath'     => 'application/uploads/productpost/');
        $this->msmarty->assignArrVal($data);
        if ($allCategory == TRUE) {
            $main = $this->msmarty->fetch($controlName . '/products_category.tpl');
        } else {
            $main = $this->msmarty->fetch($controlName . '/products_single_category.tpl');
        }

        $this->msmarty->assign('main', $main);

        $this->msmarty->view();
    }
    /**
     * @function    loadProductDetail
     *
     * @author      Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access      private
     *
     */
    private function loadProductDetail()
    {
        $controlName = $this->uri->segment(1);
        $method      = $this->uri->segment(2);
        $segment3    = $this->uri->segment(3);

        $productId  = explode('-', $segment3);
        $productId  = $productId[1];

        $lastSegment = $this->uri->segment($this->uri->total_segments());

        // Load product detail
        $prodpostObj = $this->productspost_model->loadRecord($productId, $this->lang_default);
        if ( ! is_object($prodpostObj)) {
            defaultPage($controlName);
        }
        // Load product category
        $prodCateObj = $this->products_model->loadRecord($prodpostObj->category_id, $this->lang_default);
        // Load relation of product detail
        $arrWhere = array('lang_id'     => $this->lang_default,
                          'category_id' => $prodpostObj->category_id,
                          'product_id !='  => $productId,
                          'product_show'   => 'Y');
        $relationServObj = $this->productspost_model->loadList($arrWhere, 3, 0, 'product_id', 'DESC');

        $data = array('prodCateObj'   => $prodCateObj,
                      'prodpostObj'   => $prodpostObj,
                      'otherServPost' => $relationServObj,
                      'prodImgPath'   => 'application/uploads/productpost/',
                      'categoryUrl'   => $controlName . '/products-category/',
                      'postUrl'       => $controlName . '/products-detail/');
        $this->msmarty->assignArrVal($data);

        $main = $this->msmarty->fetch($controlName . '/products_detail.tpl');
        $this->msmarty->assign('main', $main);

        $this->msmarty->view();
    }
}
?>
